package com.example.demo;


import com.alibaba.fastjson.JSON;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class Xhr {

    public static String decodeUnicode(final String dataStr) {
        int start = 0;
        int end = 0;
        final StringBuffer buffer = new StringBuffer();
        while (start > -1) {
            end = dataStr.indexOf("\\u", start + 2);
            String charStr = "";
            if (end == -1) {
                charStr = dataStr.substring(start + 2, dataStr.length());
            } else {
                charStr = dataStr.substring(start + 2, end);
            }
            // 16进制parse整形字符串。
            char letter = (char) Integer.parseInt(charStr, 16);
            buffer.append(new Character(letter).toString());
            start = end;
        }
        return buffer.toString();
    }

    public static void post() {

        HttpPost httpPost = new HttpPost("https://www.wishpost.cn/api/v3/get_channels");
//        HttpPost httpPost = new HttpPost("https://www.wishpost.cn/api/v3/account_info");


        Map<String, String> header = new HashMap<>(null);
//        header.put("Authorization", "Bearer " + token);
        header.put("Content-Type", "application/json");
//        String params =" {\n" +
//                "  \"access_token\": \"e7339bc2203f41fdbeaba7c78ba7e37e\"\n" +
//                "} ";

        String params = "{\n" +
                "  \"access_token\": \"260b543d955f4614af5f51603bc1c24d\",\n" +
                "  \"channels\": [1]\n" +
                "}";


        httpPost.setEntity(new StringEntity(params, "UTF-8"));
        Iterator var6 = header.keySet().iterator();

        while (var6.hasNext()) {
            String key = (String) var6.next();
            httpPost.addHeader(key, (String) header.get(key));
        }


        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpResponse response = null;
        try {
            response = client.execute(httpPost);
        } catch (IOException e) {
            System.out.println(e);
        }
        try {
            System.out.println(JSON.parse(EntityUtils.toString(response.getEntity(), "UTF-8")));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void post1() {

        HttpPost httpPost = new HttpPost("http://119.23.34.110:8088/default/svc/web-service/getOrder");


        Map<String, String> header = new HashMap<>(null);
        header.put("Content-Type", "application/json");


        String params = "{\"reference_no\":\"TEST180417007\",\"label_type\":\"2\"}";


        httpPost.setEntity(new StringEntity(params, "UTF-8"));
        Iterator var6 = header.keySet().iterator();

        while (var6.hasNext()) {
            String key = (String) var6.next();
            httpPost.addHeader(key, (String) header.get(key));
        }


        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpResponse response = null;
        try {
            response = client.execute(httpPost);
        } catch (IOException e) {
            System.out.println(e);
        }
        try {
            System.out.println(EntityUtils.toString(response.getEntity(), "UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
//        post();
        post1();
    }


}
